package top.chenziji.Util;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.DirectoryChooser;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class Controller {

    @FXML
    private Button btnStart;

    @FXML
    private Button btnStop;

    @FXML
    private Button btnInFilePath;

    @FXML
    private Button btnOutFilePath;

    @FXML
    private TextField outFilePath;

    @FXML
    private TextField inFilePath;

    @FXML
    private TextField password;

    @FXML
    private Label messageInfo;

    @FXML
    private Label MyStatic;

    @FXML
    public void initialize() throws IOException {
        //检查配置文件是否存在
        FileUitl.InspectConfigFileCreate();
        //加载配置文件参数
        Map map = FileUitl.ReadConfigFileCreate();

        if(map.get("outFilePath")!=null){
            String outFilePathStr = map.get("outFilePath").toString();
            outFilePath.setText(outFilePathStr);
        }
        if(map.get("inFilePath")!=null){
            String inFilePathStr = map.get("inFilePath").toString();
            inFilePath.setText(inFilePathStr);
        }

        if(map.get("password")!=null){
            String passwordStr = map.get("password").toString();
            password.setText(passwordStr);
        }

        // 给按钮绑定事件
        btnInFilePath.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                inFilePath.setText(selectPath());
                initInputText();
            }
        });

        // 给按钮绑定事件
        btnOutFilePath.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                outFilePath.setText(selectPath());
                initInputText();
            }
        });


        // 给按钮绑定事件
        btnStart.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if(!password.getText().equals("")){
                    initInputText();
                    Comm.password=password.getText();
                    Comm.outFilePath=outFilePath.getText();
                    Comm.inFilePath=inFilePath.getText();
                    MyStatic.setText("运行中");
                    MyStatic.setTextFill(Color.YELLOW);
                    CommService.start();
                }
            }
        });

        // 给按钮绑定事件
        btnStop.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                MyStatic.setText("停止中");
                MyStatic.setTextFill(Color.RED);
                CommService.stop();
            }
        });
    }

    public String selectPath(){
        // 创建 DirectoryChooser 实例
        DirectoryChooser directoryChooser = new DirectoryChooser();

        // 可选：设置默认目录路径（如果有）
        directoryChooser.setInitialDirectory(new File(System.getProperty("user.home")));

        // 弹出目录选择对话框
        File selectedDirectory = directoryChooser.showDialog(null);

        if (selectedDirectory != null) {
            String absolutePath = selectedDirectory.getAbsolutePath();
            return absolutePath;
        } else {
            // 如果用户取消了选择，显示提示信息
            System.out.println("No Directory Selected"+"No directory was selected.");
            return null;
        }
    }

    public void initInputText(){
        String inFilePathStr = inFilePath.getText();
        String outFilePathStr = outFilePath.getText();
        String passwordStr = password.getText();
        StringBuilder sb = new StringBuilder();
        sb.append("inFilePath="+inFilePathStr+"\r\n");
        sb.append("outFilePath="+outFilePathStr+"\r\n");
        sb.append("password="+passwordStr+"\r\n");
        try {
            FileUitl.writeConfigFileCreate(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
